

type IStorage=Window["localStorage"]|Window["sessionStorage"]

export default class Storage {

	storage:IStorage

	constructor(options:{storage:IStorage}){
		this.storage=options.storage
	}

	setItem(key:string,value:string,expire?:Date){
		let obj={
			data:value,
			expire
		}
		this.storage.setItem(key,JSON.stringify(obj))
	}

	getItem(key:string){
		let val =this.storage.getItem(key);
		if(!val){
			return val;
		}
		let json =JSON.parse(val);
		if(!json.expire){
			return json.data
		}
		let expire=new Date(json.expire).getTime()
		if((Date.now()-expire)>0){
			this.storage.removeItem(key);
			return null;
		}else{
			return json.data
		}
	}
}
